import { _decorator, CCBoolean, Collider2D, Component, Node, Sprite, Tween, tween, Vec3 } from 'cc';
import { FrameAnimaton } from '../../Tool/FrameAnimaton';
const { ccclass, property } = _decorator;

@ccclass('Board')
/**
 * 篮板
 */
export class Board extends Component {

    //篮板
    @property(Collider2D)
    private Board: Collider2D = null;

    //篮网
    @property(Collider2D)
    private Net0: Collider2D = null;

    //篮网
    @property(Collider2D)
    private Net1: Collider2D = null;

    //得分
    @property(Collider2D)
    private Score: Collider2D = null;

    //篮网图
    @property(Sprite)
    private NetSprtie: Sprite = null;

    @property(CCBoolean)
    private IsLeft: boolean = false;

    //进球效果
    @property(Node)
    private Spcial: Node = null;

    protected start(): void {
        this.PlayStandByAni();
        this.Spcial.active = false;
    }

    /**
     * 隐藏所有物理效果
     */
    public ShowPhysics(_isenable: boolean) {
        this.Board.enabled = _isenable;
        this.Net0.enabled = _isenable;
        this.Net1.enabled = _isenable;
        this.Score.enabled = _isenable;
    }

    protected update(dt: number): void {
        this.NetSprtie.node.setPosition(this.node.getPosition().add(this.IsLeft ? new Vec3(144, -47) : new Vec3(-144, -47)))
    }

    /**
     * 播放进球动画
     */
    public PlayAni(_cb: Function, _isnet: boolean) {
        this.ShowPhysics(false);
        if (!_isnet) this.PlaySpcialAni();
        this.NetSprtie.node.getComponent(FrameAnimaton).UpdateFrameAnimation(this.NetSprtie, _isnet ? "game_goalnet" : "game_hollownet", _isnet ? 6 : 8, 1, () => {
            this.PlayStandByAni();
            this.Spcial.active = false;
            _cb();
        }, 12)
    }

    /**
     * 播放待机动画
     */
    public PlayStandByAni() {
        this.NetSprtie.node.getComponent(FrameAnimaton).UpdateFrameAnimation(this.NetSprtie, "game_standbynet", 24, -1, null, 10)
    }

    /**
     * 播放进球效果
     */
    public PlaySpcialAni() {
        console.log("播放几次")
        this.Spcial.setScale(new Vec3(0, 0, 0))
        this.Spcial.setPosition(new Vec3((this.IsLeft ? 131 : -125), 0))
        this.Spcial.active = true;
        Tween.stopAllByTarget(this.Spcial);
        tween(this.Spcial).to(0.2, { scale: new Vec3(1, 1, 1), position: new Vec3((this.IsLeft ? 131 : -125), 12) })
            .to(0.1, { scale: new Vec3(0, 0, 0) }).call(() => {
            }).start();
    }

}


